package org.hhttp.controller;

import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import org.hhttp.core.Constants;
import org.hhttp.core.Controller;
import org.hhttp.objectPool.BoundedBlockingPool;

public class ControllerPool {
	
	private static Logger logger = Logger.getLogger(ControllerPool.class);
	
	private static BoundedBlockingPool<Controller> pool;

	public static Controller get(){
		
		Controller controller = null;
		try {
			if(pool == null){
				pool = new BoundedBlockingPool<Controller>(Constants.object_poll_num, 
						new ControllerValidator() , 
						new ControllerFactory(), 
						Executors.newCachedThreadPool());
			}
			controller = pool.get();
		} catch (Exception e) {
			logger.error(e);
		}
		return controller;
	}
	public static void release(Controller t){
		if(pool != null){
			t.reset();
			pool.release(t);
		}
	}
}
